package cn.com.shopec.erp.financing.commo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.vo.CollectSetExportVo;
import cn.com.shopec.erp.financing.vo.CollectSettDetailVO;
import cn.com.shopec.erp.financing.vo.CollectSettVO;
import cn.com.shopec.erp.financing.vo.FreightSetMentExportVo;

public class DataExport {
	private static Logger log = Logger.getLogger(DataExport.class);
	/**工作簿**/
	private HSSFWorkbook workbook = null;
	/**工作表**/
	private HSSFSheet sheet = null;
	/**工作行**/
	private HSSFRow row = null;
	/**单元 格**/
	private HSSFCell cell = null;
	private static final int SHEET_MAX_COUNT = 65535;


	/**
	 * 创建标题
	 * @param row
	 * @param title
	 */
	@SuppressWarnings("deprecation")
	public void createTitle(HSSFRow row , String [] title)
	{
		try
		{
			cell = row.createCell((short) 0);   
			
			for (int i = 0 ; i < title.length ; i++)
			{
				  cell = row.createCell((short) i);   
			      cell.setCellValue(title[i]);   
			}
		}
		catch(Exception e)
		{
			log.error("DataExport writeTitle excpeion:",e);
		}
		
	}
	
	
	/**
	 * 
	 * @param row
	 * @param title
	 */
	public void createWrite(OutputStream out )
	{
		try 
		{
			workbook.write(out);
		} 
		catch (FileNotFoundException e) 
		{
			log.error("DataExport createWrite FileNotFoundException:",e);
		}  
		catch (IOException e) 
		{
			log.error("DataExport createWrite IOException:",e);
		}
		finally
		{
			if(out != null)
			{
				try 
				{
					out.flush();
					out.close();
				} 
				catch (IOException e) 
				{
					log.error("DataExport createWrite IOException:",e);
				}
			}
		}
	
		
	}
	
	
	/**
	 * 创建联系人sheet
	 * @param dataList
	 * @param title
	 * @param out
	 */
	public void createCollectSheet(List<CollectSetExportVo> exportVos ,
								 String [] title , OutputStream out) 
	{
		
	    //默认为1
        int sheetNum = 1;
        int totalSize=exportVos.size()+20;

        //计算要创建的sheet个数
        if(totalSize % SHEET_MAX_COUNT > 0)
        {
            sheetNum = exportVos.size() / SHEET_MAX_COUNT + 1;
        }
        else
        {
            sheetNum = exportVos.size() / SHEET_MAX_COUNT;
        } 
        
       
         workbook = new HSSFWorkbook();  
         
		 /**循环工作表**/
		 for(int i = 0; i < sheetNum ; i++)
		 {
			//i ----------------工作表数量
		    sheet = workbook.createSheet(("sheet"+i)); 
	        row = sheet.createRow(0);   
	           
	        createTitle(row , title);
	        /**循环工作表的第一行**/
	        for(int j = i * SHEET_MAX_COUNT ; j < (i+1) * SHEET_MAX_COUNT ; j++ )
	        {
	        	if(j>totalSize)
	        	{
	        		break;
	        	}
	        	else
	        	{
	        		/**初始化行对象**/
		        	
		        		if(j<exportVos.size())
		        		{
		        			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT));
		        			setRowValue(row,exportVos.get(j));
		        		}
		        		else
		        		{
		        			break;
		        		}
		        		    
	        	}
	        	 	
	        	
	        }

		 }
           
        createWrite(out);
	}
	
	private void setRowValue(HSSFRow row, CollectSetExportVo collectSetExportVo) {
		if(StringUtil.isEmpty(collectSetExportVo.getAysNo()))
		{
			/**统计信息**/
			cell = row.createCell(2);
			cell.setCellValue("合计");
			cell = row.createCell(8);
			cell.setCellValue(collectSetExportVo.getTotal()+"");
		}
		else
		{
			/**内容打印**/
			cell = row.createCell(0);
			cell.setCellValue(collectSetExportVo.getLcNo());
			cell = row.createCell(1);
			cell.setCellValue(collectSetExportVo.getLcName());
			cell = row.createCell(2);
			cell.setCellValue(collectSetExportVo.getAysNo());
			cell = row.createCell(3);
			cell.setCellValue(collectSetExportVo.getOrderNo());
			cell = row.createCell(4);
			cell.setCellValue(collectSetExportVo.getCreateTime());
			cell = row.createCell(5);
			cell.setCellValue(collectSetExportVo.getStartTime());
			cell = row.createCell(6);
			cell.setCellValue(collectSetExportVo.getEndTime());
			cell = row.createCell(7);
			cell.setCellValue(collectSetExportVo.getStStatus());
			cell = row.createCell(8);
			cell.setCellValue(collectSetExportVo.getKdNo());
			cell = row.createCell(9);
			cell.setCellValue(collectSetExportVo.getTotal());
			cell = row.createCell(10);
			cell.setCellValue(collectSetExportVo.getFormaliers());
			cell = row.createCell(11);
			cell.setCellValue(collectSetExportVo.getInsures());

		}
	}


	/**
	 * 创建联系人sheet
	 * @param dataList
	 * @param title
	 * @param out
	 */
	public void createNumberSheet(List<FreightSetMentExportVo> exportVos ,
								 String [] title , OutputStream out) 
	{
		
	    //默认为1
        int sheetNum = 1;
        int totalSize=exportVos.size()+20;

        //计算要创建的sheet个数
        if(totalSize % SHEET_MAX_COUNT > 0)
        {
            sheetNum = exportVos.size() / SHEET_MAX_COUNT + 1;
        }
        else
        {
            sheetNum = exportVos.size() / SHEET_MAX_COUNT;
        } 
        
       
         workbook = new HSSFWorkbook();  
         
		 /**循环工作表**/
		 for(int i = 0; i < sheetNum ; i++)
		 {
			//i ----------------工作表数量
		    sheet = workbook.createSheet(("sheet"+i)); 
	        row = sheet.createRow(0);   
	           
	        createTitle(row , title);
	        /**循环工作表的第一行**/
	        for(int j = i * SHEET_MAX_COUNT ; j < (i+1) * SHEET_MAX_COUNT ; j++ )
	        {
	        	if(j>totalSize)
	        	{
	        		break;
	        	}
	        	else
	        	{
	        		/**初始化行对象**/
		        	
		        		if(j<exportVos.size())
		        		{
		        			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT));
		        			setRowValue(row,exportVos.get(j));
		        		}
		        		else
		        		{
		        			break;
		        		}
		        		
		        		
		    
	        	}
	        	 	
	        	
	        }

		 }
           
        createWrite(out);
	}
	

	private void setRowValue(HSSFRow row2,
			FreightSetMentExportVo freightSetMentExportVo) {
		
		if(StringUtil.isEmpty(freightSetMentExportVo.getNo()))
		{
			/**统计信息**/
			cell = row.createCell(2);
			cell.setCellValue("合计");
			cell = row.createCell(8);
			cell.setCellValue(freightSetMentExportVo.getTotal()+"");
		}
		else
		{
			/**内容打印**/
			cell = row.createCell(0);
			cell.setCellValue(freightSetMentExportVo.getSupplyName());
			cell = row.createCell(1);
			cell.setCellValue(freightSetMentExportVo.getNo());
			cell = row.createCell(2);
			cell.setCellValue(freightSetMentExportVo.getStartTime());
			cell = row.createCell(3);
			cell.setCellValue(freightSetMentExportVo.getEndTime());
			cell = row.createCell(4);
			cell.setCellValue(freightSetMentExportVo.getCycleStatus());
			cell = row.createCell(5);
			cell.setCellValue(freightSetMentExportVo.getKdNo());
			cell = row.createCell(6);
			cell.setCellValue(freightSetMentExportVo.getDevlerDate());
			cell = row.createCell(7);
			cell.setCellValue(freightSetMentExportVo.getOrderNo());
			cell = row.createCell(8);
			cell.setCellValue(""+freightSetMentExportVo.getTotal());
			cell = row.createCell(9);
			cell.setCellValue(freightSetMentExportVo.getStatementType());
			cell = row.createCell(10);
			cell.setCellValue(""+freightSetMentExportVo.getWeight());
			
		}
		
	}


	@SuppressWarnings("unused")
	private void setRowTitle(HSSFRow row, String[] title) {
		
		for(int i =0;i<title.length;i++)
		{
			cell = row.createCell(i);
			cell.setCellValue(title[i]);
		}	
		
	}


	/**
	 * 创建联系人sheet
	 * @param dataList
	 * @param title
	 * @param out
	 */
	@SuppressWarnings("deprecation")
	public void createNumberSheet(List<CollectSettVO> dataList ,
			List<CollectSettDetailVO> benqi,
								 String [] title , OutputStream out) 
	{
		
	    //默认为1
		CollectSettVO vo = dataList.get(0);
        int sheetNum = 1;
        int totalSize=dataList.size()+20;

        int ben = benqi.size();

        //计算要创建的sheet个数
        if(totalSize % SHEET_MAX_COUNT > 0)
        {
            sheetNum = dataList.size() / SHEET_MAX_COUNT + 1;
        }
        else
        {
            sheetNum = dataList.size() / SHEET_MAX_COUNT;
        } 
        
       
         workbook = new HSSFWorkbook();  
         
		
		 for(int i = 0; i < sheetNum ; i++)
		 {
			//i ----------------工作表数量
		    sheet = workbook.createSheet(("sheet"+i)); 
	        row = sheet.createRow(0);   
	           
	        createTitle(row , title);
	         // 循环填写第行数据     
	        for(int j = i * SHEET_MAX_COUNT ; j < (i+1) * SHEET_MAX_COUNT ; j++ )
	        {
                if(j < totalSize)
                {	
                	
                	if(j<=7){
                	
                	//填写结算单表头信息信息                	
                		if(j==0){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 3);
                			cell.setCellValue("代收款结算单");
                			continue;
                		}
                		if(j ==1 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("单据号");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getAysNo());
                			cell = row.createCell((short) 2);
                			cell.setCellValue("物流公司");
                			cell = row.createCell((short) 3);
                			cell.setCellValue(vo.getLcName());
                			cell = row.createCell((short) 4);
                			cell.setCellValue("物流编码");
                			cell = row.createCell((short) 5);
                			cell.setCellValue(vo.getLcNo());
                			cell = row.createCell((short) 6);
                			cell.setCellValue("结算人");
                			cell = row.createCell((short) 7);
                			cell.setCellValue(vo.getStUser());
                			
                			continue;
                		}
                		if(j ==2 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("结算金额");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getAmount());
                			cell = row.createCell((short) 2);
                			cell.setCellValue("手续费");
                			cell = row.createCell((short) 3);
                			cell.setCellValue(vo.getFormalities());
                			cell = row.createCell((short) 4);
                			cell.setCellValue("保险费");
                			cell = row.createCell((short) 5);
                			cell.setCellValue(vo.getInsurance());
                			continue;
                		}
                		if(j ==3 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("审核状态");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getAutidStatus()==0?"未审核":vo.getAutidStatus()==1?"已审核":"已拒绝");
                			cell = row.createCell((short) 2);
                			cell.setCellValue("结算状态");
                			cell = row.createCell((short) 3);
                			cell.setCellValue(vo.getStStatus()==0?"未结算":vo.getStStatus()==1?"已结算":"已作废");
                			cell = row.createCell((short) 4);
                			cell.setCellValue("结算开始日期");
                			cell = row.createCell((short) 5);
                			cell.setCellValue(vo.getStratDate());
                			cell = row.createCell((short) 6);
                			cell.setCellValue("结算结束日期");
                			cell = row.createCell((short) 7);
                			cell.setCellValue(vo.getEndDate());
                			continue;
                		}
                		if(j ==4 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getKnotDate());            		
                			continue;
                		}
                		/**本期之前表头信息**/
                		if(j ==6 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("代收款结算明细");           		
                			continue;
                		}
                		if(j ==7 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("快递单号"); 
                			cell = row.createCell((short) 1);
                			cell.setCellValue("发货日期");  
                			cell = row.createCell((short) 2);
                			cell.setCellValue("订单号");  
                			cell = row.createCell((short) 3);
                			cell.setCellValue("下单日期");  
                			cell = row.createCell((short) 4);
                			cell.setCellValue("订单金额");  
                			cell = row.createCell((short) 5);
                			cell.setCellValue("手续费");  
                			cell = row.createCell((short) 6);
                			cell.setCellValue("保险费");  
                
                			continue;
                		}
                		
                	}else{
                		if(j-8<ben){
                			
                			//本期之前
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
            				cell = row.createCell((short) 0);
            				cell.setCellValue(benqi.get(j-8).getCourierNumber()); 
            				cell = row.createCell((short) 1);
            				cell.setCellValue(benqi.get(j-8).getDeliverDate());  
            				cell = row.createCell((short) 2);
            				cell.setCellValue(benqi.get(j-8).getAsnNo());  
            				cell = row.createCell((short) 3);
            				cell.setCellValue(benqi.get(j-8).getOrderTime());  
            				cell = row.createCell((short) 4);
            				cell.setCellValue(benqi.get(j-8).getReceivable());  
            				cell = row.createCell((short) 5);
            				cell.setCellValue(benqi.get(j-8).getFormalities());  
            				cell = row.createCell((short) 6);
            				cell.setCellValue(benqi.get(j-8).getInsurance());  
           
                		
                		}
                		else{
                			break;
                		}
                		
                	}
                		        	
                	
                }
                else
                {
                	break;
                }
	        }

		 }
           
        createWrite(out);
	}
	
	
}
